/**
 * UIObject.as
 * Andy Jiang   
 * Fusion Digital Shanghai Co,Ltd. All Rights Reserved.
 * version 0.1
 * 
 */ 

package jiezi.ui
{
  import flash.display.Bitmap;
  import flash.display.BitmapData;
  import flash.display.DisplayObject
  import flash.display.Sprite
  import flash.display.Loader;
  import flash.events.Event;
  import flash.net.URLRequest

  public class Frame extends VisibleRegion
  {
    private var mSprite:Sprite;
	private var mBackdropBitmap:Loader = null;

    public static function creator(name:String):Frame
    {
      return new Frame(name)
    }

    public function Frame(name:String)
    {
      super(name)
	  addEventListener(ATTACH, this.onFrameAttached)
	  addEventListener(RESIZE, this.onFrameResized)
	  addEventListener(MOVE, this.onMoved)
      mSprite = new Sprite()
    }
	
	protected function onMoved(event:Event):void
	{
		mSprite.x = this.x;
		mSprite.y = this.y;
	}
	
	protected function onFrameAttached(event:Event):void
	{
		Frame(parent).sprite.addChild(mSprite)
	}
	
	protected function onFrameResized(event:Event):void
	{
		if ( mBackdropBitmap )
		{
			mBackdropBitmap.width = width;
			mBackdropBitmap.height = height;
		}
	}
	
    public function get sprite():Sprite
    {
      return mSprite
    }
	
	public function setBackdrop(img:String):void
	{
		var ldr:Loader = new Loader();
		var urlReq:URLRequest = new URLRequest(img);
		ldr.load(urlReq);
		
		mBackdropBitmap = ldr;
		mSprite.addChild(mBackdropBitmap)
		mBackdropBitmap.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onBackdropLoadComplete)
	}
	
	private function onBackdropLoadComplete(event:Event):void
	{
		mBackdropBitmap.width = width
		mBackdropBitmap.height = height
	}
  }
}

